# 作者：陈睿-cruise
# 2024年03月15日10时09分56秒
# 2979386061@qq.com
# 爬取抖音直播间弹幕消息
# 代码1
# from selenium import webdriver
#
# with webdriver.Chrome() as driver:
#     url='https://www.douyin.com/discover'
#     driver.get(url)
# #     input()#暂停程序
# 代码2
from selenium import webdriver
# from selenium.webdriver.common.by import By
#
# from selenium_tools import ChromeHelper
#
# with ChromeHelper() as driver:
#       url='https://live.douyin.com/489347072218'
#       driver.get(url)
#       list_message=[]
#       while True:
#           for item in driver.find_elements(By.XPATH,'//div[@class="webcast-chatroom___item webcast-chatroom___enter-done"]'):
#              if item.text not in list_message:
#               list_message.append(item.text)
#           print(list_message)

# 注意：不能同时打开两个Chrome浏览器界面，否则会产生冲突，切记切记！！！
# 代码3
# from selenium.webdriver.common.by import By
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.common.exceptions import TimeoutException
# from selenium_tools import ChromeHelper
# import time
#
# with ChromeHelper() as driver:
#     url = 'https://live.douyin.com/489347072218'
#     driver.get(url)
#
#     try:
#         # 等待聊天室消息加载
#         WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
#             (By.XPATH, '//div[@class="webcast-chatroom___item webcast-chatroom___enter-done"]')))
#     except TimeoutException:
#         print("页面加载超时，无法找到元素。")
#
#     previous_messages = set()  # 用于存储先前抓取的消息
#
#     while True:
#         # 查找所有消息元素
#         message_elements = driver.find_elements(By.XPATH,
#                                                 '//div[@class="webcast-chatroom___item webcast-chatroom___enter-done"]')
#
#         # 提取新消息的文本
#         new_messages = [item.text for item in message_elements if item.text not in previous_messages]
#
#         # 打印新消息
#         for message in new_messages:
#             print(message)
#
#         # 更新先前消息集合
#         previous_messages.update(new_messages)
#
#         # 等待一段时间以避免过于频繁地请求服务器
#         time.sleep(2)
#代码4:练习-监测抖音直播间来人
from selenium import webdriver
from selenium.webdriver.common.by import By

from selenium_tools import  ChromeHelper

with ChromeHelper() as driver:
    driver.get("https://live.douyin.com/239868649520")
    list_user=[]
    # //div[@class="webcast-chatroom___bottom-message"]
    while True:
        for item in driver.find_elements(By.XPATH,
                                         '//div[@class="webcast-chatroom___item fade fade-enter-done"]'):
            if item.text not in list_user:
                         list_user.append(item.text)
            print(list_user)